package com.rc.saas.mini.component;

import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.common.config.UpYunConfig;
import com.rc.saas.mini.common.upyun.WebUploader;
import com.rc.saas.mini.common.utils.FileHandler;
import com.rc.saas.mini.utils.AliyunOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

/**
 * @author frog
 * @date 2021/5/25
 */
@Component
public class UploadImg {

    private static final String TenantFolderTemp = "/{0}";

    @Autowired
    private UpYunConfig upYunConfig;


    /**
     * 上传图片
     *
     * @param uploadFile
     * @return
     */
    public TResponse uploadImg(String tenantCode, MultipartFile uploadFile) {
        if (uploadFile == null) {
            return TResponse.FAIL("file不存在");
        }
        //WebUploader webUploader = new WebUploader(upYunConfig);
        //WebUploader webUploader = WebUploader.getInstance(upYunConfig);
        String uploadFileName = null;
        try {
            uploadFileName = uploadFile(tenantCode, null, uploadFile,upYunConfig.getUploadEnterprisePath());
        } catch (Exception e) {
            e.printStackTrace();
            return TResponse.FAIL("操作失败");
        }
        if (StringUtils.isEmpty(uploadFileName)) {
            return TResponse.FAIL("操作失败");
        }
        TResponse success = TResponse.SUCCESS();
        success.put(uploadFileName);
        return success;
    }


    /**
     * 上传到CDN，并返回存储的路径
     *
     * @param webUploader
     * @param file
     * @return
     * @throws NoSuchAlgorithmException
     * @throws SignatureException
     * @throws InvalidKeyException
     */
    public String uploadFile(String tenantCode, WebUploader webUploader, MultipartFile file, String fileType) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        //CDN存放路径:/运营商编号/文件
        //String uploadFileName = upYunConfig.getUploadImagePath();

        String uploadFileName = tenantCode + upYunConfig.getUploadEnterprisePath();
        if (StringUtils.isEmpty(fileType)){
            uploadFileName = tenantCode + fileType;
        }
        uploadFileName = uploadFileName + FileHandler.createFileNameByTimeRole(file.getOriginalFilename());

        ////本地硬盘存放路径
        //CommonsMultipartFile cf = (CommonsMultipartFile) file;
        //DiskFileItem df = (DiskFileItem) cf.getFileItem();
        //File localFile = df.getStoreLocation();
        //boolean result = webUploader.uploadFile(uploadFileName, localFile);
        String url=AliyunOssUtil.FilePut(file,uploadFileName);


        //if (result) {
        if (url!=null) {
            //return uploadFileName;
            return url;
        } else {
            return null;
        }
    }
}
